One to Many Polymorphic Model Relationship used when a model belongs to more than one other model on a single association model. For example, If we have posts and videos tables, both need to add comments system. Then you can manage in a single table for both tables. one to many polymorphic relationship in laravel 6, laravel 7, laravel 8, laravel 9 and laravel 10 app.
In this tutorial, you can understand how to create migration with a foreign key schema for polymorphic one to many eloquent relationship, use sync with a pivot table, create records, get all data, delete, update and everything related to one to many relationship.
In this example, i will create “posts”, “videos” and “comments” table. each table is connected with each other. now we will create one to many polymorphic relationship with each other by using the laravel Eloquent Model. We will first create database migration, then model, retrieve records and then how to create records too. So you can also see database table structure on below screen.
One to Many Polymorphic Relationship will use “morphMany()” and “morphTo()” for relation.
Create Migrations:
Now we have to create migration of “posts”, “videos” and “comments” table. we will also add foreign key with posts, videos table. so let’s create like as below:
posts table migration:
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string("name");
$table->timestamps();
});
videos table migration:
Schema::create('videos', function (Blueprint $table) {
$table->increments('id');
$table->string("name");
$table->timestamps();
});
comments table migration:
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->string("body");
$table->integer('commentable_id');
$table->string("commentable_type");
$table->timestamps();
});
Create Models:
Here, we will create Post, Video and Comment table model. we will also use “morphMany()” and “morphTo()” for relationship of both model.
Post Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
/**
* Get all of the post's comments.
*/
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
}
Video Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Video extends Model
{
/**
* Get all of the post's comments.
*/
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
}
Comment Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
/**
* Get all of the owning commentable models.
*/
public function commentable()
{
return $this->morphTo();
}
}
Retrieve Records:
$post = Post::find(1);
dd($post->comments);
$video = Video::find(1);
dd($video->comments);
Create Records:
$post = Post::find(1);
$comment = new Comment;
$comment->body = "Hi ItSolutionStuff.com";
$post->comments()->save($comment);
Read Also: Laravel One to One Eloquent Relationship Tutorial
$video = Video::find(1);
$comment = new Comment;
$comment->body = "Hi ItSolutionStuff.com";
$video->comments()->save($comment);
I hope you understand of one to many polymorphic relationship…